<template>
  <el-container class="home-container">
<!--    头部区域-->
    <el-header class="home-header">
      <div class="title">
        <span>电商后台管理系统</span>
      </div>
      <div class="right-content">
        <span class="welcome">欢迎您：{{ $store.state.username }}</span>
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!--      页面主体区-->
    <el-container>
<!--      侧边栏-->
      <el-aside class="home-aside" :width=" isCollapse? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
            class="home-menu"
            :unique-opened = "true"
            active-text-color="#409EFF"
            background-color="#333744"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath"
            text-color="#fff">
          <!-- 一级菜单-->
          <el-submenu :index="item.id + ''" v-for="(item) in menuList" :key="item.id">
            <!--一级菜单的模板-->
            <template #title>
<!--              一级菜单图标-->
              <i :class="iconsObj[item.id]"></i>
<!--              一级菜单文本-->
              <span>{{ item.authName }}</span>
            </template>
<!--            二级菜单-->
            <el-menu-item :index=" $router.path !== '/' + item.path + '' ? item.path + '' : '' " v-for="(item) in item.children" :key="item.id" @click="saveNavState(item.path)">
              <template #title>
                <!--              二级菜单图标-->
                <i class="el-icon-menu"></i>
                <!--              二级菜单文本-->
                <span>{{ item.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
<!--右侧内容主体-->
      <el-main class="home-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data(){
    return{
      menuList:[],
      iconsObj:{
        '125':'el-icon-s-custom',
        '103':'el-icon-s-cooperation',
        '101':'el-icon-s-shop',
        '102':'el-icon-s-order',
        '145':'el-icon-s-data'
      },
      // 是否折叠收起菜单
      isCollapse:false,
      // 当前激活的菜单
      activePath:''
    }
  },

  mounted() {
    this.getMenuList()
    this.getNavState()
    console.log(this.$route);
  },
  methods:{
    logout(){
      localStorage.removeItem('token')
      localStorage.removeItem('activePath')
      this.$router.push('/login')
    },
    //获取左侧菜单列表
   async getMenuList(){
     const { data:res } = await this.$http.get('menus')
     const {meta:meta } = res
     if(meta.status === 200){
       this.menuList = res.data
     }else {
       this.$message.error(meta.msg)
       localStorage.removeItem('username')
       localStorage.removeItem('token')
       this.$router.push('/login')
     }
     console.log(res);
   },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    // 点击二级菜单，保存链接的激活状态
    saveNavState(activePath){
      localStorage.setItem('activePath',activePath)
    },
    // 激活当前路由对应的菜单状态
    getNavState(){
      this.activePath = localStorage.getItem('activePath')
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.home-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  //padding-left: 0;
  align-items: center;
  .right-content{
    display: flex;
    //justify-content: space-around;
    //justify-content: space-around;
    align-items: center;

  }
  .welcome{
    display: block;
    color:#eeeeee;
    margin-right: 10px;
    text-align: center;
    line-height: 100%;
  }
  .title{
    display: flex;
    align-items: center;
    font-size: 20px;
    color: #fff;
  }
}
.home-aside{
  background-color: #333744;
  .home-menu{
    border-right: none;
  }
  .toggle-button{
    background-color: #4A5064;
    font-size: 10px;
    text-align: center;
    line-height: 24px;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.home-main{
  background-color: #eaedf1;
}

</style>